package com.zhongjy.urtime.ui;

import java.util.List;
import java.util.ArrayList;
import java.util.Map;
import java.util.Set;

import com.urtime.debug.UrtimeDebug;
import com.zhongjy.urtime.data.UrtimeEvent;
import com.zhongjy.urtime.data.UrtimeHoldEventData;
import com.zhongjy.urtime.data.UrtimeSMS;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.TextView;
import android.widget.ListView;

public class HoldEventDetail extends Activity {
	public static final int EDIT_EXISTING=100;
	public static final int EVENT_CHANGED=-6;
	private TextView nameText;
	private TextView timeText;
	private TextView contentText;
	private ListView replyListView;
	private Button editBtn;
	private Button finalInviteBtn;
	private List<String> replyList;
	private ArrayAdapter<String> adapter;
	private UrtimeEvent event;
	private Bundle bundle;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.organizingevent);
		editBtn = (Button) findViewById(R.id.button1);
		finalInviteBtn = (Button) findViewById(R.id.button2);
		replyListView = (ListView) findViewById(R.id.listView1);
		bundle=this.getIntent().getExtras();
		event=UrtimeHoldEventData.getEvent(bundle.getString("name"));
		nameText=(TextView)findViewById(R.id.textView5);
		timeText=(TextView)findViewById(R.id.textView6);
		contentText=(TextView)findViewById(R.id.textView8);
		editBtn.setOnClickListener(new Button.OnClickListener() {
			public void onClick(View v) {
				editExisting();
			}
		});
		finalInviteBtn.setOnClickListener(new Button.OnClickListener() {
			public void onClick(View v) {
				Intent intent = new Intent();
				Bundle mb=new Bundle();
				intent.setClass(HoldEventDetail.this, ConfirmUrtimeActivity.class);
				mb.putString("name", event.getEventName());
				intent.putExtras(mb);
				startActivity(intent);
				finish();
			}
		});
		refreshAll();
	}
	protected void editExisting(){
		Intent intent = new Intent();
		intent.setClass(HoldEventDetail.this, HoldNewEvent.class);
		intent.putExtra("oldName", event.getEventName());
		startActivityForResult(intent,EDIT_EXISTING);
	}
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		// TODO Auto-generated method stub
		if(requestCode==EDIT_EXISTING){
			String newName=data.getStringExtra("newName");
			if(newName==null)return;
			if(!newName.equals(event.getEventName()))UrtimeHoldEventData.deleteEvent(event);
			event=UrtimeHoldEventData.getEvent(newName);
			refreshAll();
			setResult(EVENT_CHANGED);
		}
		super.onActivityResult(requestCode, resultCode, data);
	}

	private void refreshAll(){
		nameText.setText(event.getEventName());
		timeText.setText(event.getStrTime());//maybe changed to calendar
		contentText.setText(event.getEventDescription());
		replyList=getReplyList(event.getEventName());
		adapter = new ArrayAdapter<String>(this,
				android.R.layout.simple_list_item_1, replyList);
		replyListView.setAdapter(adapter);
	}
	
	private List<String> getReplyList(String eventName) {
		// TODO Auto-generated method stub
		List<String> replyList=null;
		if (eventName != null) {
			UrtimeEvent urtimeEvent = UrtimeHoldEventData.getEvent(eventName);
			Map<String, String> contacts = urtimeEvent.getContact();
			Set<String> contactsPhones = contacts.keySet();
			replyList=new ArrayList<String>();
			for (String cp : contactsPhones) {
				String cn = contacts.get(cp);
				cn = "\n" + cn + ":"
						+ UrtimeSMS.getContactReply(HoldEventDetail.this, cp,urtimeEvent.getCreateTime());
				replyList.add(cn);
			}
		}
		return replyList;
	}
}

